package stardust.product.utils.baby;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.htmlparser.util.Translate;
import stardust.core.io.IOUtils;
import stardust.core.support.conversion.StringConversion;

public class Amazon
{

  public static void main(String[] args) throws IOException
  {
    String file = "report-" + StringConversion.parseDate(new Date(), "yyyy年MM月dd日hh时mm分ss秒") + ".csv";
    IOUtils.write(file, new byte[0], false);
    IOUtils.write(file, "来源文件,订单编号,商品编码,尺码,销售总额,收货人,收货地址,联系电话\n".getBytes(), true);
    
    File[] files = new File(".").listFiles();
    for (File i : files)
    {
      if (i.isDirectory() || !(i.getName().toLowerCase().endsWith(".htm") || i.getName().toLowerCase().endsWith(".html")))
      {
        continue;
      }
      
      byte[] data = IOUtils.read(i);
      String html = new String(data, "UTF-8");
  
      Amazon.analyseOrderInfo(file, i.getName(), html);
    }
  }
  
  public static void analyseOrderInfo(String output, String srcFile, String html) throws IOException
  {
    try
    {
      // 提取订单编号。
      String orderNoTextBegin = "订单编号：";
      String orderNoTextEnd = "\n";
      int orderNoBegin = html.indexOf(orderNoTextBegin);
      int orderNoEnd = html.indexOf(orderNoTextEnd, orderNoBegin);
      String orderNo = html.substring(orderNoBegin + orderNoTextBegin.length(), orderNoEnd).trim();
    
      // 提取商品信息。
      List<String> good = new ArrayList<String>();
      int goodOffset = 0;
      
      try
      {
        for (int i = 0; i < 10; i++)
        {
          String goodTextBegin0 = "<span class=\"smaller\"><strong><a href=";
          String goodTextBegin1 = ">";
          String goodTextEnd0 = "</a>";
          int goodBegin0 = html.indexOf(goodTextBegin0, goodOffset);
          if (goodBegin0 == -1) break;
          int goodBegin = html.indexOf(goodTextBegin1, goodBegin0 + goodTextBegin0.length());
          int goodEnd = html.indexOf(goodTextEnd0, goodBegin);
          String goodTempString = Translate.decode(html.substring(goodBegin + goodTextBegin1.length(), goodEnd));
          goodOffset = goodBegin;
          good.add(goodTempString);
        }
      }
      catch (Exception e)
      {
        System.out.println(e);
      }
      
      // 提取商品价格。
      List<String> sale = new ArrayList<String>();
      int saleOffset = 0;
      
      try
      {
        for (int i = 0; i < 10; i++)
        {
          String saleTextBegin0 = "<td class=\"smaller\" align=\"right\" nowrap>￥";
          String saleTextEnd0 = "</td>";
          int saleBegin = html.indexOf(saleTextBegin0, saleOffset);
          if (saleBegin == -1) break;
          int saleEnd = html.indexOf(saleTextEnd0, saleBegin);
          sale.add(html.substring(saleBegin + saleTextBegin0.length(), saleEnd));
          saleOffset = saleEnd;
        }
      }
      catch (Exception e)
      {
        System.out.println(e);
      }
      
      // 提取配送地址。
      String address, man, phone;
      
      String addressTextBegin = "配送地址</strong><br>";
      String addressTextEnd = "</td>";
      int addressBegin = html.indexOf(addressTextBegin);
      int addressEnd = html.indexOf(addressTextEnd, addressBegin);
      String addressTempString = html.substring(addressBegin + addressTextBegin.length(), addressEnd);
      
      String[] addressTempStringArray = addressTempString.replaceAll("<br>", "\n").split("\n");
      man = addressTempStringArray[0];
      address = Translate.decode(addressTempStringArray[1] + addressTempStringArray[2]);
      phone = addressTempStringArray[3].substring(3);
      
      
      for (int i = 0; i < good.size() && i < sale.size(); i++)
      {
        String line = "\"" + srcFile + "\",\"" + orderNo.replaceAll("\"", "\\\"") + "\",\"" + getBianma(good.get(i)).replaceAll("\"", "\\\"") + 
                      "\",\"" + getChima(good.get(i)).replaceAll("\"", "\\\"") + "\",\"" + sale.get(i).replaceAll("\"", "\\\"") + 
                      "\",\"" + man.replaceAll("\"", "\\\"") + "\",\"" + address.replaceAll("\"", "\\\"") + "\",\"" + phone.replaceAll("\"", "\\\"") + "\"\n";
        
        IOUtils.write(output, line.getBytes(), true);
      }
      
      System.out.println("成功：" + srcFile);
    }
    catch (Exception e)
    {
      System.out.println("失败：" + srcFile);
    }
  }
  
  public static String getChima(String str)
  {
    String chimaTextBegin0 = "（";
    String chimaTextBegin1 = "(";
    
    String chimaTextEnd0 = "）";
    String chimaTextEnd1 = ")";
    
    int chimaBegin = str.indexOf(chimaTextBegin0) == -1 ? str.indexOf(chimaTextBegin1) : str.indexOf(chimaTextBegin0);
    int chimaEnd = str.indexOf(chimaTextEnd0) == -1 ? str.indexOf(chimaTextEnd1) : str.indexOf(chimaTextEnd0);
    
    if (chimaBegin == -1)
    {
      return str;
    }
    else if (chimaEnd == -1)
    {
      return str.substring(chimaBegin + chimaTextBegin0.length());
    }
    else
    {
      return str.substring(chimaBegin + chimaTextBegin0.length(), chimaEnd).trim();
    }
  }
  
  public static String getBianma(String str)
  {
    String chimaTextBegin0 = "（";
    String chimaTextBegin1 = "(";
    
    int chimaBegin = str.indexOf(chimaTextBegin0) == -1 ? str.indexOf(chimaTextBegin1) : str.indexOf(chimaTextBegin0);
    
    if (chimaBegin == -1)
    {
      return str;
    }
    else
    {
      int chimaOffset = 0;
      
      for (int i = chimaBegin - 1; i >= 0; i--)
      {
        Character c = (Character) str.charAt(i);
        
        if (!Character.isWhitespace(c) && !Character.isDigit(c) && !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
        {
          chimaOffset = i;
          break;
        }
      }
      
      return str.substring(chimaOffset + 1, chimaBegin).trim();
    }
  }

}
